package com.jxsm.service.impl;
import com.jxsm.dao.ContractDao;
import com.jxsm.dao.ExportDao;
import com.jxsm.dao.export.ExportproductDao;
import com.jxsm.page.Page;
import com.jxsm.pojo.*;
import com.jxsm.service.ExportService;
import com.jxsm.util.UtilFuns;
import com.jxsm.pojo.ContractProductVO;
import com.jxsm.pojo.ContractVO;
import com.jxsm.pojo.ExtCproductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2016/11/9.
 */
@Service
public class ExportServiceimpl implements ExportService {
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExportproductDao exportproductDao;
    public void insert(String[] contractIds) {

        String contractNos="";
        for (int i=0; i<contractIds.length;i++){
            ContractVO contract= contractDao.view(contractIds[i]);
            contractNos+=contract.getContractNo()+" ";
        }
        contractNos=UtilFuns.delLastChar(contractNos);
        Export export=new Export();
        export.setId(UUID.randomUUID().toString());
        export.setContractIds(UtilFuns.joinStr(contractIds,","));//拼接合同的字符串
        export.setCustomerContract(contractNos);
        exportDao.insert(export);
        for(int i=0;i<contractIds.length;i++ ) {
            ContractVO contractVO = contractDao.view(contractIds[i]);
            for (ContractProductVO contractProduct : contractVO.getContractProducts()) {
                Exportproduct exportproduct = new Exportproduct();
                exportproduct.setId(UUID.randomUUID().toString());
                exportproduct.setExportid(export.getId());


                //数据搬家，将合同下的对应的货物信息写入到报运下的货物信息中
                exportproduct.setFactoryId(contractProduct.getFactory().getId());
                exportproduct.setFactoryName(contractProduct.getFactory().getFactoryName());
                exportproduct.setProductNo(contractProduct.getProductNo());
                exportproduct.setPackingUnit(contractProduct.getPackingUnit());
                exportproduct.setCnumber(contractProduct.getCnumber());
                exportproduct.setBoxNum(contractProduct.getBoxNum());
                exportproduct.setPrice(contractProduct.getPrice());
                exportproductDao.insert(exportproduct);
                //处理附件的信息
                for(ExtCproductVO extCproductVO:contractProduct.getExtCproducts()){
                    ExtEproduct extEproduct=new ExtEproduct();
                    BeanUtils.copyProperties(extCproductVO,extEproduct);
                    extEproduct.setId(UUID.randomUUID().toString());
                    extEproduct.setExportProductId(exportproduct.getId());
                    extEproduct.setFactoryId(extCproductVO.getFactory().getId());
                    extEproduct.setFactoryName(extCproductVO.getFactory().getFactoryName());

                }
            }
        }
    }

    public void updateExport(Export entity) {
        exportDao.update(entity);
    }

    public void deleteById(Serializable id) {
        exportDao.deleteById(id);
    }

    public void deleteExport(Serializable[] id) {
        exportDao.delete(id);
    }

    public List<Export> findPage(Page page) {
        return exportDao.findPage(page);
    }

    public List<Export> find(Map map) {
        return exportDao.findAll(map);
    }

    public Export getExport(Serializable id) {
        return exportDao.get(id);
    }

    public void start(Serializable[] id) {
        Map<String ,Object> map=new HashMap();
        map.put("state",1);
        map.put("ids",id);
        exportDao.updateState(map);
    }

    public void stop(Serializable[] id) {
        Map<String ,Object> map=new HashMap();
        map.put("state",0);
        map.put("ids",id);
        exportDao.updateState(map);
    }

    public List<Contract> contract(Map map) {
        return  contractDao.findAll(map);
    }

    public void updateExportProduct(String[] mr_id, Integer[] mr_orderNo, Integer[] mr_cnumber, Double[] mr_grossWeight, Double[] mr_netWeight, Double[] mr_sizeLength, Double[] mr_sizeWidth, Double[] mr_sizeHeight, Double[] mr_exPrice, Double[] mr_tax, Integer[] mr_changed) {
       for(int i=0;i<mr_id.length;i++){
           Exportproduct  ep=exportproductDao.get(mr_id[i]);
           ep.setOrderNo(mr_orderNo[i]);
           ep.setCnumber(mr_cnumber[i]);
           ep.setGrossWeight(mr_grossWeight[i]);
           ep.setNetWeight(mr_netWeight[i]);
           ep.setSizeLength(mr_sizeLength[i]);
           ep.setSizeWidth(mr_sizeWidth[i]);
           ep.setSizeHeight(mr_sizeHeight[i]);
           ep.setExPrice(mr_exPrice[i]);
           ep.setTax(mr_tax[i]);
           exportproductDao.update(ep);
       }

    }
}
